<template>
  <div class="app-container">
    <el-form class="form-container" :model="form" :rules="rules" ref="refForm" label-width="100px">

      <el-form-item label="游戏名称：" prop="gameName">
        <el-input v-model="form.gameName">
        </el-input>
      </el-form-item>

      <el-form-item label="游戏类别：" prop="subType">
        <el-select v-model="form.subType" filterable placeholder="请选择游戏类别">
          <el-option v-for="item in consts.DIGITAL_GAME_TYPE"
                     :key="item.value"
                     :label="item.title"
                     :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="游戏线数：" prop="lineNum">
        <el-input v-model="form.lineNum">
        </el-input>
      </el-form-item>
      <el-form-item label="游戏图片：" prop="pic">
        <el-upload
          class="avatar-uploader"
          list-type="picture-card"
          :action="uploadApi"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload">
          <img v-if="form.logo" :src="form.logo">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        <el-tip>*支持gif、jpg、png格式，大小不超过2M</el-tip>
        <el-input v-model="form.logo" type="hidden"></el-input>
      </el-form-item>

      <el-button v-loading="loading" style="margin-left: 10px;" type="primary"
                 @click="doSave({
                 action: 'saveForm',
                 form: {
                 name: 'refForm',
                 scrollToError: true
                 }})">确定
      </el-button>
    </el-form>
  </div>
</template>

<script>

import Cookies from 'js-cookie';
import {
  gameManagerSubGameSaveApi as saveApi,
} from '@/services/api/game';
import { form } from '@/build';

export default {
  name: 'sub-game-edit',

  mixins: [form],

  props: [
    'game-id',
    'game-name',
    'sub-type',
    'logo',
    'line-num',
  ],

  data() {
    return {
      uploadApi: `${api.UPLOAD_PIC}?token=${Cookies.get(consts.CACHE_TOKEN)}&uid=${Cookies.get(consts.CACHE_UID)}`,
      form: {
        gameId: this.gameId,
        gameName: this.gameName,
        subType: this.subType,
        logo: this.logo,
        lineNum: this.lineNum,
      },
      rules: {
        gameName: { required: true, message: '游戏名称必须填写' },
        subType: { required: true, message: '游戏类别必须选择' },
        logo: { required: true, message: '请上传图片' },
        lineNum: { required: true, message: '请填写游戏线数' },
      },
    };
  },
  mounted() {
    this.bindSave(saveApi, 'saveForm', 'form', {
      afterSuccess: () => {
        this.$emit('on-success');
      },
    });
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.form.logo = res.data;
    },
    beforeAvatarUpload(file) {
      const isJPEG = file.type === 'image/jpeg';
      const isPNG = file.type === 'image/png';
      const isJPG = file.type === 'image/jpg';
      const isGIF = file.type === 'image/gif';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG && !isJPEG && !isPNG && !isGIF) {
        this.$message.error('上传头像图片只能是 JPG/png/jpeg/gif 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return (isJPG || isJPEG || isPNG || isGIF) && isLt2M;
    },
  },
};
</script>

<style lang="scss" scoped>
  .avatar {
    width: 100px;
  }
  .avatar-uploader{
    img{
      width: 224px;
      height: 190px;
    }
  }
</style>
